﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace PortableVersion.Core.General
{
    public static class PopularExtensions
    {
        public static TOut NullSafe<TIn, TOut>(this TIn obj, Func<TIn, TOut> memberAction)
        {
            return (obj != null) ? memberAction(obj) : default(TOut);
        }
        
        public static void AssertAll<T>(this IEnumerable<T> list, Func<T, bool> assertion, Action<T> onAssestionFailed)
        {
            foreach (var i in list.Where(i => !assertion(i)))
            {
                onAssestionFailed(i);
                break;
            }
        }

        public static void AssertNotNull(this Object obj, string message, Action onAssertionFailed = null) 
        {
            if (obj == null)
            {
                if (onAssertionFailed != null)
                {
                    onAssertionFailed();
                }
                else
                {
                    throw new ArgumentNullException(message);
                }
            }
        }
    }
}
